/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SalesReportFrame.java
 *
 * Created on Mar 30, 2011, 2:43:39 PM
 */

package gbbmgmt;

import java.awt.*;
import javax.swing.*;
import java.util.*;
import org.jdesktop.application.Action;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import java.lang.Math;
import java.text.*;

/**
 *
 * @author dchapman
 */
public class SalesReportFrame extends javax.swing.JFrame {

    //declare global variables.
    private DrawingPanel drawpanel;
    private Dimension area;
    private Rectangle graphBorder;
    private Rectangle oval;
    private Color ovalColor;

    //variable place holder
    private int iyear = 0;
    private int imonth = 0;

    //Fixed variables.
    private int img_width=600;
    private int img_height=400;
    private int margins=20;
    private int horizontal_lines=20;

    //Calculated Variables.
    private int graph_width=img_width-(margins*2);
    private int graph_height=img_height-(margins*2);
    private int horizontal_gap = 0;

    public ArrayList<String[]> Sales;
    public ArrayList<String[]> SalesSort;
    public int totalNum = 0;
    public double maxval = 0;
    public ArrayList<String[]> Info;

    public ArrayList<String[]> Customers;

    NumberFormat f = new DecimalFormat("#0.00");
    NumberFormat f1 = new DecimalFormat("#0");


    /** Creates new form SalesReportFrame */
    public SalesReportFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jDateChooserStart = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        jDateChooserStop = new com.toedter.calendar.JDateChooser();
        jButtonShowGraph = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jCheckBoxSD = new javax.swing.JCheckBox();
        jCheckBoxLRC = new javax.swing.JCheckBox();
        jCheckBoxFY = new javax.swing.JCheckBox();
        jCheckBoxMT = new javax.swing.JCheckBox();
        jPanelGraphArea = new javax.swing.JPanel();
        jScrollPaneGraph = new javax.swing.JScrollPane();
        jCheckBoxAll = new javax.swing.JCheckBox();
        jRadioButtonOutside = new javax.swing.JRadioButton();
        jRadioButtonCust = new javax.swing.JRadioButton();
        jRadioButtonTRoom = new javax.swing.JRadioButton();
        jRadioButtonAll = new javax.swing.JRadioButton();
        jComboBoxCust = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getResourceMap(SalesReportFrame.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jDateChooserStart.setName("jDateChooserStart"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jDateChooserStop.setName("jDateChooserStop"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getActionMap(SalesReportFrame.class, this);
        jButtonShowGraph.setAction(actionMap.get("ShowGraph")); // NOI18N
        jButtonShowGraph.setText(resourceMap.getString("jButtonShowGraph.text")); // NOI18N
        jButtonShowGraph.setName("jButtonShowGraph"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jCheckBoxSD.setForeground(resourceMap.getColor("jCheckBoxSD.foreground")); // NOI18N
        jCheckBoxSD.setSelected(true);
        jCheckBoxSD.setText(resourceMap.getString("jCheckBoxSD.text")); // NOI18N
        jCheckBoxSD.setName("jCheckBoxSD"); // NOI18N
        jCheckBoxSD.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jCheckBoxSDMouseClicked(evt);
            }
        });
        jCheckBoxSD.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jCheckBoxSDPropertyChange(evt);
            }
        });

        jCheckBoxLRC.setForeground(resourceMap.getColor("jCheckBoxLRC.foreground")); // NOI18N
        jCheckBoxLRC.setSelected(true);
        jCheckBoxLRC.setText(resourceMap.getString("jCheckBoxLRC.text")); // NOI18N
        jCheckBoxLRC.setName("jCheckBoxLRC"); // NOI18N
        jCheckBoxLRC.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jCheckBoxLRCMouseClicked(evt);
            }
        });

        jCheckBoxFY.setForeground(resourceMap.getColor("jCheckBoxFY.foreground")); // NOI18N
        jCheckBoxFY.setSelected(true);
        jCheckBoxFY.setText(resourceMap.getString("jCheckBoxFY.text")); // NOI18N
        jCheckBoxFY.setName("jCheckBoxFY"); // NOI18N
        jCheckBoxFY.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jCheckBoxFYMouseClicked(evt);
            }
        });

        jCheckBoxMT.setForeground(resourceMap.getColor("jCheckBoxMT.foreground")); // NOI18N
        jCheckBoxMT.setSelected(true);
        jCheckBoxMT.setText(resourceMap.getString("jCheckBoxMT.text")); // NOI18N
        jCheckBoxMT.setName("jCheckBoxMT"); // NOI18N
        jCheckBoxMT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jCheckBoxMTMouseClicked(evt);
            }
        });

        jPanelGraphArea.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanelGraphArea.border.title"))); // NOI18N
        jPanelGraphArea.setName("jPanelGraphArea"); // NOI18N

        jScrollPaneGraph.setName("jScrollPaneGraph"); // NOI18N
        jScrollPaneGraph.setPreferredSize(new java.awt.Dimension(600, 400));
        jScrollPaneGraph.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jScrollPaneGraphMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jScrollPaneGraphMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanelGraphAreaLayout = new javax.swing.GroupLayout(jPanelGraphArea);
        jPanelGraphArea.setLayout(jPanelGraphAreaLayout);
        jPanelGraphAreaLayout.setHorizontalGroup(
            jPanelGraphAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneGraph, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
        );
        jPanelGraphAreaLayout.setVerticalGroup(
            jPanelGraphAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneGraph, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
        );

        jCheckBoxAll.setForeground(resourceMap.getColor("jCheckBoxAll.foreground")); // NOI18N
        jCheckBoxAll.setSelected(true);
        jCheckBoxAll.setText(resourceMap.getString("jCheckBoxAll.text")); // NOI18N
        jCheckBoxAll.setName("jCheckBoxAll"); // NOI18N
        jCheckBoxAll.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jCheckBoxAllMouseClicked(evt);
            }
        });

        buttonGroup1.add(jRadioButtonOutside);
        jRadioButtonOutside.setText(resourceMap.getString("jRadioButtonOutside.text")); // NOI18N
        jRadioButtonOutside.setName("jRadioButtonOutside"); // NOI18N
        jRadioButtonOutside.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButtonOutsideMouseClicked(evt);
            }
        });

        buttonGroup1.add(jRadioButtonCust);
        jRadioButtonCust.setText(resourceMap.getString("jRadioButtonCust.text")); // NOI18N
        jRadioButtonCust.setName("jRadioButtonCust"); // NOI18N
        jRadioButtonCust.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButtonCustMouseClicked(evt);
            }
        });

        buttonGroup1.add(jRadioButtonTRoom);
        jRadioButtonTRoom.setText(resourceMap.getString("jRadioButtonTRoom.text")); // NOI18N
        jRadioButtonTRoom.setName("jRadioButtonTRoom"); // NOI18N
        jRadioButtonTRoom.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButtonTRoomMouseClicked(evt);
            }
        });

        buttonGroup1.add(jRadioButtonAll);
        jRadioButtonAll.setSelected(true);
        jRadioButtonAll.setText(resourceMap.getString("jRadioButtonAll.text")); // NOI18N
        jRadioButtonAll.setName("jRadioButtonAll"); // NOI18N
        jRadioButtonAll.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButtonAllMouseClicked(evt);
            }
        });

        jComboBoxCust.setName("jComboBoxCust"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDateChooserStart, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDateChooserStop, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 182, Short.MAX_VALUE)
                .addComponent(jButtonShowGraph)
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 743, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelGraphArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBoxSD)
                    .addComponent(jCheckBoxLRC)
                    .addComponent(jCheckBoxFY)
                    .addComponent(jCheckBoxMT)
                    .addComponent(jCheckBoxAll)
                    .addComponent(jRadioButtonCust)
                    .addComponent(jRadioButtonOutside)
                    .addComponent(jRadioButtonTRoom)
                    .addComponent(jRadioButtonAll))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jComboBoxCust, 0, 723, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(jDateChooserStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jDateChooserStop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonShowGraph))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanelGraphArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBoxSD)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCheckBoxLRC)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCheckBoxFY)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCheckBoxMT)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCheckBoxAll)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jRadioButtonAll)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButtonTRoom)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButtonOutside)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButtonCust)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxCust, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckBoxSDPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jCheckBoxSDPropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBoxSDPropertyChange

    private void jCheckBoxSDMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCheckBoxSDMouseClicked
        // TODO add your handling code here:
        ShowGraph();
    }//GEN-LAST:event_jCheckBoxSDMouseClicked

    private void jCheckBoxLRCMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCheckBoxLRCMouseClicked
        // TODO add your handling code here:
        ShowGraph();
    }//GEN-LAST:event_jCheckBoxLRCMouseClicked

    private void jCheckBoxFYMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCheckBoxFYMouseClicked
        // TODO add your handling code here:
        ShowGraph();
    }//GEN-LAST:event_jCheckBoxFYMouseClicked

    private void jCheckBoxMTMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCheckBoxMTMouseClicked
        // TODO add your handling code here:
        ShowGraph();
    }//GEN-LAST:event_jCheckBoxMTMouseClicked

    private void jCheckBoxAllMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCheckBoxAllMouseClicked
        // TODO add your handling code here:
        ShowGraph();
    }//GEN-LAST:event_jCheckBoxAllMouseClicked

    private void jRadioButtonAllMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButtonAllMouseClicked
        // TODO add your handling code here:
        ShowGraph();
    }//GEN-LAST:event_jRadioButtonAllMouseClicked

    private void jRadioButtonTRoomMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButtonTRoomMouseClicked
        // TODO add your handling code here:
        ShowGraph();
    }//GEN-LAST:event_jRadioButtonTRoomMouseClicked

    private void jRadioButtonOutsideMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButtonOutsideMouseClicked
        // TODO add your handling code here:
        ShowGraph();
    }//GEN-LAST:event_jRadioButtonOutsideMouseClicked

    private void jRadioButtonCustMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButtonCustMouseClicked
        // TODO add your handling code here:
        ShowGraph();
    }//GEN-LAST:event_jRadioButtonCustMouseClicked

    private void jScrollPaneGraphMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jScrollPaneGraphMousePressed
        // TODO add your handling code here:
        int x = evt.getX();
        int y = evt.getY();
        int xdif = 10000;
        int ydif = 10000;
        //String for the value to be displayed.
        String dispVal = "";
        //Parse String and find the nearest point for display.
        for(String s[] : Info){
            int tmpx = Integer.parseInt(s[0].trim());
            int tmpy = Integer.parseInt(s[1].trim());
            int X = Math.abs(x-tmpx);
            int Y = Math.abs(y-tmpy);
            if((X<=xdif)&&(Y<=ydif)){
                xdif = X;
                ydif = Y;
                dispVal = s[2] + " BBL";
            }
        }

        //drawpanel.setToolTipText("x = " + Integer.toString(x) + ", y = " + Integer.toString(y));
        Graphics g = drawpanel.getGraphics();
        g.setColor(Color.YELLOW);
        g.fillRect(x-2, y-15, 80, 20);
        g.setColor(Color.BLACK);
        g.drawRect(x-2, y-15, 80, 20);
        g.drawString(dispVal, x, y);
    }//GEN-LAST:event_jScrollPaneGraphMousePressed

    private void jScrollPaneGraphMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jScrollPaneGraphMouseReleased
        // TODO add your handling code here:
        ShowGraph();
    }//GEN-LAST:event_jScrollPaneGraphMouseReleased

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SalesReportFrame().setVisible(true);
            }
        });
    }

    public final class DrawingPanel extends javax.swing.JPanel{
        protected void paintComponent(Graphics g){
            super.paintComponent(g);

            //Draw border for graph.
            g.setColor(Color.BLACK);
            g.drawRect(margins, margins, img_width-(margins*2), img_height-(margins*2));

            //Draw lines on graph
            horizontal_gap = graph_height / horizontal_lines;
            for(int i=1; i<=horizontal_lines; i++){
                int y = img_height - margins - horizontal_gap * i;
                g.drawLine(margins, y, img_width-(margins), y);
            }

            //if all beers is checked, draw a black line representing sales.
        }
    }

    public void redrawgraph(){
        Graphics g = drawpanel.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, img_width, img_height);

        g.setColor(Color.BLACK);
        g.drawRect(margins, margins, img_width-(margins*2), img_height-(margins*2));

        //Draw lines on graph
        horizontal_gap = graph_height / horizontal_lines;
        for(int i=1; i<=horizontal_lines; i++){
            int y = img_height - margins - horizontal_gap * i;
            g.drawLine(margins, y, img_width-(margins), y);
        }
    }

    public void LoadList(){
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        try{
            Date start = df.parse("01/07/2010");
            jDateChooserStart.setDate(start);
        } catch (ParseException e){
            e.printStackTrace();
        }

        DBA.openDBCon();
        Customers = DBA.RunSQLQuery("SELECT CId,CName FROM Customer");
        DBA.closeDBCon();

        for(String s[] : Customers){
            jComboBoxCust.addItem(s[1]);
        }

        //Get todays date.
        java.util.Date dt = new java.util.Date();
        //java.sql.Date todaysdate = new java.sql.Date( dt.getTime());
        jDateChooserStop.setDate(dt);
        
        oval = new Rectangle(20,30,10,20);
        ovalColor = new Color(0,0,0);
        area = new Dimension(400,200);

        drawpanel = new DrawingPanel();
        jScrollPaneGraph.setSize(img_width,img_height);
        jScrollPaneGraph.setViewportView(drawpanel);

        drawpanel.setBackground(Color.WHITE);
        drawpanel.setPreferredSize(area);
        drawpanel.revalidate();
        drawpanel.repaint();
    }

    @org.jdesktop.application.Action
    public void ShowGraph() {
        //Redraw screen to clean.
        redrawgraph();
        totalNum = 0;

        //get dates specified.
        Date strt = jDateChooserStart.getDate();
        java.sql.Date StartDate = new java.sql.Date( strt.getTime() );

        Date stp = jDateChooserStop.getDate();
        java.sql.Date StopDate = new java.sql.Date( stp.getTime() );


        //Read all sales for given dates from database.
        DBA.openDBCon();
        String query = "SELECT SId,CustID,BeerID,SDate,NumHalf,NumQtr,NumSix,NumTT,NumTW,NumSF" +
                " FROM Sales WHERE SDate>='" +
                StartDate.toString() + "' AND SDate<='" +
                StopDate.toString() + "'";
        Sales = DBA.RunSQLQuery(query);
        DBA.closeDBCon();

        //Determine Month and Year of start date for sorting.
        String sDate = StartDate.toString();
        String month = sDate.substring(5,sDate.length()-3);
        String year = sDate.substring(0,(sDate.length()-6));
        imonth = Integer.parseInt(month.trim());
        iyear = Integer.parseInt(year.trim());

        SalesSort = new ArrayList<String[]>();

        Info = new ArrayList<String[]>();

        int custSelect = 0;
        String Itm = jComboBoxCust.getSelectedItem().toString();

        for(String s[] : Customers){
            if (s[1].equals(Itm)) {
                custSelect = Integer.parseInt(s[0].trim());
            }
        }

        //Parse through sales and separate into appropriate arrays.
        for(String s[] : Sales){
            //Find which month / year record is for and add it to bucket.
            String modDate = s[3].substring(0,(s[3].length()-14));
            String dateYear = modDate.substring(0,modDate.length()-3);
            String dateMonth = modDate.substring(5,modDate.length());
            int idateYear = Integer.parseInt(dateYear.trim());
            int idateMonth = Integer.parseInt(dateMonth.trim());

            //Parse through array and find larger datevalue for array list size.
            int datenum = (idateMonth - imonth + 1) + (12*(idateYear - iyear));

            //Get Values of kegs.
            int nhlf = Integer.parseInt(s[4].trim());
            int nqtr = Integer.parseInt(s[5].trim());
            int nsix = Integer.parseInt(s[6].trim());
            int ntt = Integer.parseInt(s[7].trim());
            int ntw = Integer.parseInt(s[8].trim());
            int nsf = Integer.parseInt(s[9].trim());

            double volume = ((double)nhlf/2) + ((double)nqtr/4) + ((double)nsix/6);
            volume += ((double)ntt*(double)Global.TwentyTwoCase) * Global.TTtoBBL;
            volume += ((double)ntw*(double)Global.TwelveCase) * Global.TWtoBBL;
            volume += ((double)nsf*(double)Global.SevenFiftyCase) * Global.SFtoBBL;

            //Fill out new array list.
            String[] record = new String[10];
            record[0] = Integer.toString(datenum);
            record[1] = s[1];
            record[2] = s[2];
            record[3] = Double.toString(volume);

            int custID = Integer.parseInt(s[1].trim());

            //add code to only add what radio buttons say to add.
            if(jRadioButtonAll.isSelected()){
                SalesSort.add(record);
                if(totalNum<datenum){
                    totalNum=datenum;
                }
            }
            if(jRadioButtonOutside.isSelected() && custID != 14 && custID != 15){
                SalesSort.add(record);
                if(totalNum<datenum){
                    totalNum=datenum;
                }
            }
            if(jRadioButtonTRoom.isSelected() && (custID==14 || custID == 15)){
                SalesSort.add(record);
                if(totalNum<datenum){
                    totalNum=datenum;
                }
            }
            if(jRadioButtonCust.isSelected() && custID == custSelect){
                SalesSort.add(record);
                if(totalNum<datenum){
                    totalNum=datenum;
                }
            }
        }
        //Sales Sort now contains all of the information from the query in usable form.

        //int response = JOptionPane.showConfirmDialog(null,tmp,"Warning",JOptionPane.WARNING_MESSAGE);

        //Reset the max val.
        maxval = 0;
        getMaxVal();

        //Draw values for lines based on the max val.
        Graphics g = drawpanel.getGraphics();
        horizontal_gap = graph_height / horizontal_lines;    
        double subval = maxval / (horizontal_lines);
        String dispval = "";
        int x = horizontal_lines+1;
        for(int i=1; i<=(horizontal_lines+1); i++){
            if(i<horizontal_lines+1){
                dispval = f1.format(maxval - (subval * (x-1)));
            } else {
                dispval = f1.format(maxval);
            }
            int y = img_height - margins - horizontal_gap * i;
            g.drawString(dispval, 2, y+22);
            x--;
        }

        //Draw values for lines based on the date.
        int total_dots = totalNum;
        int gap = (graph_width - total_dots) / (total_dots + 1);

        int reconmonth = imonth;
        int reconyear = iyear;

        for(int i=0; i<total_dots; i++){
            int nx = margins + gap + (i*gap) - 20;
            int ny = (margins*2) + graph_height - 5;
            //calculate the string to display based on number.
            //int datenum = (idateMonth - imonth + 1) + (12*(idateYear - iyear));
            if(reconmonth>12){
                reconmonth = 1;
                reconyear++;
            }
            String show = Integer.toString(reconmonth) + "/" + Integer.toString(reconyear);
            g.drawString(show, nx, ny);
            reconmonth++;
        }


        //Draw All Data
        if(jCheckBoxAll.isSelected()){
            drawData();
        }
        //Draw data for selected beers in set colors.
        if(jCheckBoxLRC.isSelected()){
            drawSelected(1,Color.RED);
        }
        if(jCheckBoxSD.isSelected()){
            drawSelected(2,Color.YELLOW);
        }
        if(jCheckBoxFY.isSelected()){
            drawSelected(3,Color.BLUE);
        }
        if(jCheckBoxMT.isSelected()){
            drawSelected(4,Color.MAGENTA);
        }

        //Setup variables for graph in this function.
        graphBorder = new Rectangle(20,30,10,20);
    }

    public void getMaxVal(){
        //If all are checked the just run this.
        if(jCheckBoxAll.isSelected()){
            double[] vols = new double[totalNum];
            for(String s[] : SalesSort){
                int idx = Integer.parseInt(s[0].trim())-1;
                double value = Double.valueOf(s[3].trim()).doubleValue();
                double volume = vols[idx] + value;
                if(maxval<volume)maxval=volume;
                vols[idx] = volume;
            }
        }
        if(jCheckBoxLRC.isSelected()){
            double[] vols = new double[totalNum];
            for(String s[] : SalesSort){
                if(Integer.parseInt(s[2].trim()) == 1){
                    int idx = Integer.parseInt(s[0].trim())-1;
                    double value = Double.valueOf(s[3].trim()).doubleValue();
                    double volume = vols[idx] + value;
                    if(maxval<volume)maxval=volume;
                    vols[idx] = volume;
                }
            }
        }
        if(jCheckBoxSD.isSelected()){
            double[] vols = new double[totalNum];
            for(String s[] : SalesSort){
                if(Integer.parseInt(s[2].trim()) == 2){
                    int idx = Integer.parseInt(s[0].trim())-1;
                    double value = Double.valueOf(s[3].trim()).doubleValue();
                    double volume = vols[idx] + value;
                    if(maxval<volume)maxval=volume;
                    vols[idx] = volume;
                }
            }
        }
        if(jCheckBoxFY.isSelected()){
            double[] vols = new double[totalNum];
            for(String s[] : SalesSort){
                if(Integer.parseInt(s[2].trim()) == 3){
                    int idx = Integer.parseInt(s[0].trim())-1;
                    double value = Double.valueOf(s[3].trim()).doubleValue();
                    double volume = vols[idx] + value;
                    if(maxval<volume)maxval=volume;
                    vols[idx] = volume;
                }
            }
        }
        if(jCheckBoxMT.isSelected()){
            double[] vols = new double[totalNum];
            for(String s[] : SalesSort){
                if(Integer.parseInt(s[2].trim()) == 4){
                    int idx = Integer.parseInt(s[0].trim())-1;
                    double value = Double.valueOf(s[3].trim()).doubleValue();
                    double volume = vols[idx] + value;
                    if(maxval<volume)maxval=volume;
                    vols[idx] = volume;
                }
            }
        }
    }

    public void drawData(){
        //Draw black line for all sales volume.
        //Go through sales sort and parse volumes into buckets.
        double[] vols = new double[totalNum];
        //String tmp = Integer.toString(totalNum);
        

        double ratio = 0;

        for(String s[] : SalesSort){
            int idx = Integer.parseInt(s[0].trim())-1;
            double value = Double.valueOf(s[3].trim()).doubleValue();
            double volume = vols[idx] + value;
            if(maxval<volume)maxval=volume;
            vols[idx] = volume;
        }

        ratio = (double)graph_height / maxval;

        int total_dots = totalNum;
        int gap = (graph_width - total_dots) / (total_dots + 1);

        //int response = JOptionPane.showConfirmDialog(null,tmp,"Warning",JOptionPane.WARNING_MESSAGE);

        Graphics g = drawpanel.getGraphics();
        g.setColor(Color.BLACK);

        int oldx = 0;
        int oldy = 0;

        //Draw dots
        for(int i=0; i<total_dots; i++){
            int x = margins + gap + (i*gap);
            int y = margins + graph_height - (int)(vols[i] * ratio);
            g.fillOval(x-3, y-3, 6, 6);
            
            //fill in relavent info.
            //Array of strings for displaying info on points.
            String[] infoRecs = new String[3];
            infoRecs[0] = Integer.toString(x);
            infoRecs[1] = Integer.toString(y);
            infoRecs[2] = f.format(vols[i]);
            Info.add(infoRecs);
            //int rese = JOptionPane.showConfirmDialog(null,Double.toString(vols[i]),"Warning",JOptionPane.WARNING_MESSAGE);

            //Connect the dots with a line.
            if(i>0){
                g.drawLine(oldx, oldy, x, y);
            }
            oldx = x;
            oldy = y;
        }
    }

    public void drawSelected(int item, Color c){
        //Draw black line for all sales volume.
        //Go through sales sort and parse volumes into buckets.
        double[] vols = new double[totalNum];
        String tmp = Integer.toString(totalNum);

        double ratio = 0;

        for(String s[] : SalesSort){
            if(Integer.parseInt(s[2].trim()) == item){
                int idx = Integer.parseInt(s[0].trim())-1;
                double value = Double.valueOf(s[3].trim()).doubleValue();
                double volume = vols[idx] + value;
                if(maxval<volume)maxval=volume;
                vols[idx] = volume;
            }
        }

        ratio = (double)graph_height / maxval;

        int total_dots = totalNum;
        int gap = (graph_width - total_dots) / (total_dots + 1);

        //int response = JOptionPane.showConfirmDialog(null,tmp,"Warning",JOptionPane.WARNING_MESSAGE);

        Graphics g = drawpanel.getGraphics();
        g.setColor(c);

        int oldx = 0;
        int oldy = 0;

        //Draw dots
        for(int i=0; i<total_dots; i++){
            int x = margins + gap + (i*gap);
            int y = margins + graph_height - (int)(vols[i] * ratio);
            g.fillOval(x-3, y-3, 6, 6);
            //fill in relavent info.
            //Array of strings for displaying info on points.
            String[] infoRecs = new String[3];
            infoRecs[0] = Integer.toString(x);
            infoRecs[1] = Integer.toString(y);
            infoRecs[2] = f.format(vols[i]);
            Info.add(infoRecs);
            //Connect the dots with a line.
            if(i>0){
                g.drawLine(oldx, oldy, x, y);
            }
            oldx = x;
            oldy = y;
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonShowGraph;
    private javax.swing.JCheckBox jCheckBoxAll;
    private javax.swing.JCheckBox jCheckBoxFY;
    private javax.swing.JCheckBox jCheckBoxLRC;
    private javax.swing.JCheckBox jCheckBoxMT;
    private javax.swing.JCheckBox jCheckBoxSD;
    private javax.swing.JComboBox jComboBoxCust;
    private com.toedter.calendar.JDateChooser jDateChooserStart;
    private com.toedter.calendar.JDateChooser jDateChooserStop;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanelGraphArea;
    private javax.swing.JRadioButton jRadioButtonAll;
    private javax.swing.JRadioButton jRadioButtonCust;
    private javax.swing.JRadioButton jRadioButtonOutside;
    private javax.swing.JRadioButton jRadioButtonTRoom;
    private javax.swing.JScrollPane jScrollPaneGraph;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

}
